<?php
	class FornDAO{
		protected $db;

		public function __construct ($db){
			$this->db = $db;
		}

		public function setDb($db){
			$this->db = $db;
		}

		public function getDb(){
			return $this->db;
		}

		/* Passar o objeto Forn */
		public function addForn($f){
			$sql = "insert into forn (razao, cnpj, fantasia, tel, e_mail, ativo)
					values (".($f->getRazao() ? "'{$f->getRazao()}'" : "null").",'{$f->getCnpj()}','{$f->getFantasia()}','{$f->getTel()}','{$f->getEmail()}','{$f->getAtivo()}')";
			$res = $this->getDb()->query($sql);
			
			return $res;		
		}

		/* Passar o objeto Forn */
		public function setForn($f){
			$sql = "update forn set razao = ".($f->getRazao() ? "'{$f->getRazao()}'" : "null").", cnpj = '{$f->getCnpj()}', fantasia = '{$f->getFantasia()}',
									tel = '{$f->getTel()}', e_mail = '{$f->getEmail()}', ativo = '{$f->getAtivo()}'
					where forn = {$f->getId()}";			
			$res = $this->getDb()->query($sql);					
			return $res;			
		}

		/* Passar o objeto Forn */
		public function delForn($f){
			$sql = "delete from forn where forn = {$f->getId()}";
			$res = $this->getDb()->query($sql);			
			return $res;
		}

		/* Passar a PK do Forn para retorno do objeto */
		public function getForn($id){
			$sql = "select FORN, RAZAO, TEL, E_MAIL, CNPJ, FANTASIA, ATIVO
					from forn 
					where forn = {$id}";
			$res = $this->getDb()->query($sql);
			$r = $res[0];

			if($r){
				$f = new Forn($r['FORN'],$r['RAZAO'],$r['CNPJ'],$r['FANTASIA'],$r['TEL'],$r['E_MAIL'],$r['ATIVO']);
				return $f;
			}
			else{
				return false;
			}				
		}

		/* Passar um array para cada argumento do where */
		public function getLista($where=array()){
			$flista = array();
			$where = ($where ? "where ".implode(" and ",$where) : "");

			$sql = "select FORN, RAZAO, TEL, E_MAIL, CNPJ, FANTASIA, ATIVO
					from forn
					{$where}
					order by RAZAO";			
			$res = $this->getDb()->query($sql);			

			if($res[0]){
				foreach($res as $r){
					$flista[] = new Forn($r['FORN'],$r['RAZAO'],$r['CNPJ'],$r['FANTASIA'],$r['TEL'],$r['E_MAIL'],$r['ATIVO']);
				}
			}				
			return $flista;
		}
	}
?>